<template>
  <div>
    <div class="process-design" style="display: flex; height: 92dvh">
      <my-process-designer
        v-model="xmlString"
        v-bind="controlForm"
        keyboard
        ref="processDesigner"
        :events="['element.click', 'connection.added', 'connection.removed', 'connection.changed']"
        @element-click="elementClick"
        @init-finished="initModeler"
        @element-contextmenu="elementContextmenu"
        @save="onSaveProcess"
      />

      <my-process-penal :bpmn-modeler="modeler" :prefix="controlForm.prefix" class="process-panel" />

      <!-- demo config -->
      <div class="demo-control-bar">
        <div class="open-model-button" @click="controlDrawerVisible = true">
          <setting-outlined />
        </div>
      </div>
      <a-drawer v-model:open="controlDrawerVisible" width="400px" title="偏好设置" destroy-on-close>
        <a-form :model="controlForm" size="small" label-width="100px" class="control-form" @submit.prevent>
          <a-form-item label="流程ID">
            <a-input v-model="controlForm.processId" @change="reloadProcessDesigner(true)" />
          </a-form-item>
          <a-form-item label="流程名称">
            <a-input v-model="controlForm.processName" @change="reloadProcessDesigner(true)" />
          </a-form-item>
          <a-form-item label="流转模拟">
            <a-switch v-model="controlForm.simulation" inactive-text="停用" active-text="启用" @change="reloadProcessDesigner()" />
          </a-form-item>
          <a-form-item label="禁用双击">
            <a-switch v-model="controlForm.labelEditing" inactive-text="停用" active-text="启用" @change="changeLabelEditingStatus" />
          </a-form-item>
          <a-form-item label="自定义渲染">
            <a-switch v-model="controlForm.labelVisible" inactive-text="停用" active-text="启用" @change="changeLabelVisibleStatus" />
          </a-form-item>
          <a-form-item label="流程引擎">
            <a-radio-group v-model:value="controlForm.prefix" @change="reloadProcessDesigner()">
              <a-radio value="camunda">camunda</a-radio>
              <a-radio value="flowable">flowable</a-radio>
              <a-radio value="activiti">activiti</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="工具栏">
            <a-radio-group v-model:value="controlForm.headerButtonSize">
              <a-radio value="small">small</a-radio>
              <a-radio value="default">default</a-radio>
              <a-radio value="large">large</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </a-drawer>
    </div>
  </div>
</template>

<script>
  import MyProcessDesigner from '/@/package/designer';
  // import MyProcessPalette from '@/package/palette';
  import MyProcessPenal from '/@/package/penal';
  // 自定义渲染（隐藏了 label 标签）
  import CustomRenderer from '/@/modules/custom-renderer';
  // 自定义元素选中时的弹出菜单（修改 默认任务 为 用户任务）
  import CustomContentPadProvider from '/@/package/designer/plugins/content-pad';
  // 自定义左侧菜单（修改 默认任务 为 用户任务）
  import CustomPaletteProvider from '/@/package/designer/plugins/palette';

  import '/@/package/theme/index.css';
  import 'bpmn-js/dist/assets/diagram-js.css';
  import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
  import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css';

  export default {
    name: 'ProcessDesigner',
    props: {
      bpmnXml: {
        type: String,
        required: true,
      },
      designerForm: {
        type: Object,
        required: true,
      },
    },
    components: {
      MyProcessDesigner,
      MyProcessPenal,
    },
    data() {
      return {
        height: document.documentElement.clientHeight - 120.5 + 'px;',
        xmlString: this.bpmnXml,
        modeler: null,
        controlDrawerVisible: false,
        infoTipVisible: false,
        pageMode: false,
        controlForm: {
          processId: this.designerForm.processKey || '',
          processName: this.designerForm.processName || '',
          simulation: true,
          labelEditing: false,
          labelVisible: false,
          prefix: 'flowable',
          headerButtonSize: 'default',
          additionalModel: [CustomContentPadProvider, CustomPaletteProvider],
        },
        addis: {
          CustomContentPadProvider,
          CustomPaletteProvider,
        },
      };
    },
    methods: {
      reloadProcessDesigner(notDeep) {
        this.controlForm.additionalModel = [];
        for (const key in this.addis) {
          if (this.addis[key]) {
            this.controlForm.additionalModel.push(this.addis[key]);
          }
        }
        !notDeep && (this.xmlString = undefined);
        this.reloadIndex += 1;
        this.modeler = null; // 避免 panel 异常
      },
      changeLabelEditingStatus(status) {
        this.addis.labelEditing = status ? { labelEditingProvider: ['value', ''] } : false;
        this.reloadProcessDesigner();
      },
      changeLabelVisibleStatus(status) {
        this.addis.customRenderer = status ? CustomRenderer : false;
        this.reloadProcessDesigner();
      },
      elementClick(element) {
        this.element = element;
      },
      initModeler(modeler) {
        setTimeout(() => {
          this.modeler = modeler;
        }, 20);
      },
      elementContextmenu(element) {},
      onSaveProcess(saveData) {
        this.$emit('save', saveData);
      },
    },
  };
</script>

<style>
  body {
    overflow: hidden;
    margin: 0;
    box-sizing: border-box;
  }

  /* 滚动条样式 */
  body::-webkit-scrollbar-track-piece,
  body *::-webkit-scrollbar-track-piece {
    background-color: #fff;
    -webkit-border-radius: 0;
  }

  body::-webkit-scrollbar,
  body *::-webkit-scrollbar {
    width: 10px;
    height: 8px;
  }

  body::-webkit-scrollbar-thumb:vertical,
  body *::-webkit-scrollbar-thumb:vertical {
    height: 50px;
    background-color: rgba(153, 153, 153, 0.5);
    -webkit-border-radius: 4px;
    outline: 2px solid #fff;
    outline-offset: -2px;
    border: 2px solid #fff;
  }

  body::-webkit-scrollbar-thumb,
  body *::-webkit-scrollbar-thumb {
    background-color: rgba(159, 159, 159, 0.3);
    -webkit-border-radius: 4px;
  }

  body::-webkit-scrollbar-thumb:hover,
  body *::-webkit-scrollbar-thumb:hover {
    background-color: rgba(159, 159, 159, 0.5);
    -webkit-border-radius: 4px;
  }

  .demo-control-bar {
    position: fixed;
    right: 8px;
    bottom: 48px;
    z-index: 1;
  }

  .open-model-button {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    font-size: 32px;
    background: rgba(64, 158, 255, 1);
    color: #ffffff;
    cursor: pointer;
  }
</style>
